package com.hj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 修改部分springMvc的默认配置
 *
 * @author haoshen
 * @version 1.0
 */
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {
    /**
     * 额外配置路径对应的映射
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/main").setViewName("main");
        registry.addViewController("/header").setViewName("common/header");
        registry.addViewController("/welcome").setViewName("welcome");
        registry.addViewController("/listUser").setViewName("listUser");
        registry.addViewController("/category").setViewName("category");
        registry.addViewController("/categoryAdd").setViewName("categoryAdd");
        registry.addViewController("/listPro").setViewName("listPro");
        registry.addViewController("/productAdd").setViewName("productAdd");
        registry.addViewController("/mainIndex").setViewName("hjMainIndex");
        registry.addViewController("/register").setViewName("register");
        registry.addViewController("/product").setViewName("product");
        registry.addViewController("/detail").setViewName("detail");
        registry.addViewController("/cart").setViewName("cart");
        registry.addViewController("/order").setViewName("order");
        registry.addViewController("/search").setViewName("search");
        registry.addViewController("/userDetail").setViewName("userDetail");
        registry.addViewController("/changePsw").setViewName("changePsw");
        registry.addViewController("/manageUserDetail").setViewName("manager/userDetail");
        registry.addViewController("/manageChangeCateName").setViewName("manager/changeCateName");
        registry.addViewController("/listAllPro").setViewName("manager/listAllPro");
        registry.addViewController("/changePro").setViewName("manager/changePro");
        registry.addViewController("/listOrder").setViewName("manager/listOrder");
    }

    /**
     * 这里配置静态资源文件的路径导包都是默认的直接导入就可以
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        //图片服务器
        registry.addResourceHandler("/image/**").addResourceLocations("file:D:/image/");
        super.addResourceHandlers(registry);
    }

    /**
     * 注册自己设置的国际化设置
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

    /**
     * 注册登录拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
        registration.addPathPatterns("/**");
        registration.excludePathPatterns("/");
        registration.excludePathPatterns("/index");
        registration.excludePathPatterns("/static/**");
        registration.excludePathPatterns("/error");
        registration.excludePathPatterns("/image/**");
        registration.excludePathPatterns("/mainIndex");
        registration.excludePathPatterns("/register");
        registration.excludePathPatterns("/changePsw");
        registration.excludePathPatterns("/detail");
        registration.excludePathPatterns("/user/getUser");
        registration.excludePathPatterns("/pro/cheap");
        registration.excludePathPatterns("/cate/listCate");
        registration.excludePathPatterns("/cate/moreCate");
        registration.excludePathPatterns("/pro/new");
        registration.excludePathPatterns("/pro/proDetail");
        registration.excludePathPatterns("/cart/addPro");
        registration.excludePathPatterns("/user/login");
        registration.excludePathPatterns("/user/addUser");
        registration.excludePathPatterns("/user/verCode");
        registration.excludePathPatterns("/user/isNameExist");
        registration.excludePathPatterns("/user/changePsw");
        registration.excludePathPatterns("/user/imageCode");
    }
}
